/*-\|/-\|/-\|/-\|/
Support File of 
PCB functions for R2 

Version 1.0
|/-\|/-\|/-\|/-\*/
#include <stdio.h>
#include "mrj_supt.h"
#include "mpx_supt.h"
#include "R3.H"
pcb* searchPCBbyName(char*);

pcb* setPCB(char* name, int priority, int Pclass){
        //Allocate memory for PCB
        pcb *newPCB;
        newPCB = allocPCB();
        //Compare name string using findPCB function to check if name is unique
	if(searchPCBbyName(name)!=NULL){
                printf("ERROR: Process already exists\n");
                return NULL;
        }
        else
                strcpy(newPCB->name, name);
        
        //Check to see if priority is valid; between -128 and 127
        if((priority < -128) || (priority > 127)){
                printf("ERROR: Process priority is invalid, please enter a value"
                       "between -128 and 127\n");
                return NULL;
        }
        else
                newPCB->priority = priority;
        //Check to see if the PCB Class is valid
        if((Pclass != PCB_CLASS_SYSTEM) && (Pclass != PCB_CLASS_APP)){
                printf("ERROR: Process class is invalid, please enter a correct value:\n"
                       "0 for System Process, 1 for Application Process\n");
                return NULL;
        }
        else
                newPCB->Pclass = Pclass;
        //Set up initial states
        newPCB->state = PCB_STATE_RDY;
        newPCB->susp = PCB_FLAG_FREE;
        newPCB->memSize = PCB_STACK_SIZE;
        //newPCB->stackT = NULL;
        //newPCB->stackB = NULL;
        newPCB->locAddr = NULL;
        newPCB->excAddr = NULL;
        return newPCB;
}

pcb* allocPCB(){
		pcb *newPCB = (pcb *)sys_alloc_mem(sizeof(pcb));
		newPCB->stackB = (unsigned char *)sys_alloc_mem(PCB_STACK_SIZE*sizeof(unsigned char));
		newPCB->stackT = (newPCB->stackB) + (PCB_STACK_SIZE) - (sizeof(context));
        return newPCB;
}


